package org.jeecg.modules.qyxx.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.Rest;
import org.jeecg.modules.fk.entity.DjNsrxx;
import org.jeecg.modules.qyxx.entity.DjNsrxxSyncRecord;
import org.jeecg.modules.qyxx.entity.req.BaseRequest;
import org.jeecg.modules.qyxx.entity.resp.BaseResponse;
import org.jeecg.modules.qyxx.mapper.DjNsrxxSyncRecordMapper;
import org.jeecg.modules.qyxx.service.IDjNsrxxService;
import org.jeecg.modules.qyxx.service.IDjNsrxxSyncRecordService;
import org.jeecg.modules.qyxx.service.NsrzxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
// @DS("multi-datasource1")申报
@Service
public class DjNsrxxSyncRecordServiceImpl extends ServiceImpl<DjNsrxxSyncRecordMapper, DjNsrxxSyncRecord> implements IDjNsrxxSyncRecordService {

    @Autowired
    NsrzxService nsrzxService;

    @Autowired
    IDjNsrxxService djNsrxxService;

    @Override
    public Rest<String> getnsrxx(String nsrsbh, String areaCode) {
//        Rest<BaseResponse> nsrjbxx= new Rest<>();
//        BaseRequest baseRequest =new BaseRequest();
//        try {
//            baseRequest.setShxydm(nsrsbh);
//            baseRequest.setAreaCode(areaCode);
//            nsrjbxx=nsrzxService.nsrjbxxcx(baseRequest);
//        }catch (Exception e){
//            log.error("调用/getCompanyInfo接口报错--->请求参数:{}", JSON.toJSONString(baseRequest));
//        }

        return null;
    }

    @Override
    public boolean saveDjnsrxx(DjNsrxx djNsrxx) {
        return false;
    }

    @Override
    public boolean saveDjnsrxxFx(DjNsrxx djNsrxx) {
        return false;
    }

    @Override
    public boolean saveDjnsrxxSyncRecord(DjNsrxxSyncRecord djNsrxxSyncRecord, String status, String remark) {
        return false;
    }

    /*@Autowired
    IDjNsrxxFxService djNsrxxFxService;

    @Autowired
    IDjNsrzyxxSyncRecordService djNsrzyxxSyncRecordService;

    @Autowired
    NsrzxService nsrzxService;*/


   /* @Override
    public Rest<String> getnsrxx(String nsrsbh, String areaCode) {
        Rest<BaseResponse> nsrjbxx= new Rest<>();
        BaseRequest baseRequest =new BaseRequest();
        try {
            baseRequest.setShxydm(nsrsbh);
            baseRequest.setAreaCode(areaCode);
            nsrjbxx=nsrzxService.nsrjbxxcx(baseRequest);
        }catch (Exception e){
            log.error("调用/getCompanyInfo接口报错--->请求参数:{}", JSON.toJSONString(baseRequest));
        }
        if (!"0".equals(nsrjbxx.getErrCode())){
            //写入dj_nsrxx_sync_record表同步情况 调用feign接口返回失败
            DjNsrxxSyncRecord djNsrxxSyncRecord = new DjNsrxxSyncRecord();
            djNsrxxSyncRecord.setNsrsbh(nsrsbh);
            djNsrxxSyncRecord.setAreaCode(areaCode);
            boolean b = saveDjnsrxxSyncRecord(djNsrxxSyncRecord, nsrjbxx.getErrCode(), nsrjbxx.getMessage());

            return new Rest<>(false,"同步失败");
        }
        JSONObject parse = new JSONObject();
        DjNsrxx djNsrxx = new DjNsrxx();
        try {
            //解析报文
            HashMap data=(HashMap) nsrjbxx.getData().getData();
            HashMap taxML=(HashMap) data.get("taxML");
            ArrayList nsrjbxxList=(ArrayList) taxML.get("nsrjbxx");
            ArrayList yhxxList=(ArrayList) taxML.get("yhxx");
            HashMap yhxxlbMap =new HashMap();
            if(yhxxList != null && yhxxList.size()>0){
                HashMap yhxxMap =(HashMap) yhxxList.get(0);
                ArrayList yhxxlbList=(ArrayList) yhxxMap.get("yhxxlb");
                if(yhxxlbList!=null && yhxxlbList.size()>0){
                     yhxxlbMap =(HashMap) yhxxlbList.get(0);
                }
            }
            if (nsrjbxxList.size()>0){
                HashMap nsrjbxxMap =(HashMap) nsrjbxxList.get(0);
                ArrayList<HashMap> nsrxxlbList=(ArrayList) nsrjbxxMap.get("nsrxxlb");
                if (nsrxxlbList.size()>0){
                    for(HashMap nsrxxlbMap : nsrxxlbList){
                        if("03".equals(nsrxxlbMap.get("nsrztDm"))){
                            String djrqStr=(String) nsrxxlbMap.get("djrq");
                            if (StringUtils.isNotEmpty(djrqStr)){
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                Date djrqDate=sdf.parse(djrqStr);//字符串转date
                                ZoneId zoneId = ZoneId.systemDefault();
                                LocalDateTime djrq = LocalDateTime.ofInstant(djrqDate.toInstant(), zoneId);//date转localDateTime
                                nsrxxlbMap.put("djrq",djrq); //覆盖原先的djrq
                            }

                            try {
                                BeanUtils.populate(djNsrxx,nsrxxlbMap);
                            }catch (Exception e){
                                log.error("map转bean失败--->抛出异常:{},map:{},bean:{}",e,nsrxxlbMap,djNsrxx);
                            }

                            ArrayList<HashMap> nsrxxkzList=(ArrayList) nsrxxlbMap.get("nsrxxkz");
                            if (nsrxxkzList.size()>0){
                                HashMap nsrxxkzMap=(HashMap) nsrxxkzList.get(0); //从表信息
                                try {
                                    BeanUtils.populate(djNsrxx,nsrxxkzMap);
                                }catch (Exception e){
                                    log.error("map转bean失败--->抛出异常:{},map:{},bean:{}",e,nsrxxkzMap,djNsrxx);
                                }
                            }
                            djNsrxx.setDjxh(yhxxlbMap.get("djxh").toString());//设置yhxx的djxh

                        }
                    }


                }
            }

        }catch (Exception e){
            log.error("/getCompanyInfo接口返回报文解析报错--->返回报文:{}",nsrjbxx.getData());
            DjNsrxxSyncRecord djNsrxxSyncRecord = new DjNsrxxSyncRecord();
            djNsrxxSyncRecord.setNsrsbh(nsrsbh);
            djNsrxxSyncRecord.setAreaCode(areaCode);
            boolean saveDjnsrxxSyncRecord=saveDjnsrxxSyncRecord(djNsrxxSyncRecord, "200", "无数据");
        }

        if (!BeanUtil.isEmpty(djNsrxx)){
            djNsrxx.setAreaCode(areaCode);
            djNsrxx.setSjcjSj(LocalDateTime.now());
            //绑定企业时设定redis，有效期一天,保存到名为风险的数据库
//            if(RedisUtil.get(RedisKeyConstant.redisFxNsrsbhKey + nsrsbh)!=null){
                saveDjnsrxxFx(djNsrxx);
//            }
            boolean saveDjnsrxx=saveDjnsrxx(djNsrxx);

            if (saveDjnsrxx){
                DjNsrxxSyncRecord djNsrxxSyncRecord = new DjNsrxxSyncRecord();
                //djNsrxx中的nsrsbh 跟入参时nsrsbh可能不同
                djNsrxxSyncRecord.setNsrsbh(nsrsbh);
                djNsrxxSyncRecord.setAreaCode(areaCode);
                boolean saveDjnsrxxSyncRecord=saveDjnsrxxSyncRecord(djNsrxxSyncRecord, "100", "成功");
                //更新纳税人资格 2020-12-02 zhouxiaojian
                djNsrzyxxSyncRecordService.getNsrzyxx(djNsrxx.getNsrsbh(),djNsrxx.getDjxh(),djNsrxx.getAreaCode());
                //纳税人主要信息采集
                NsrzyxxcxRequest nsrzyxxcxRequest =new NsrzyxxcxRequest();
                org.springframework.beans.BeanUtils.copyProperties(djNsrxx,nsrzyxxcxRequest);
                djNsrxxService.updateNsrzglxDm(nsrzyxxcxRequest);
            }
            //暂不记录存库失败
//            else {
//                DjNsrxxSyncRecord djNsrxxSyncRecord = new DjNsrxxSyncRecord();
//                djNsrxxSyncRecord.setNsrsbh(djNsrxx.getNsrsbh());
//                djNsrxxSyncRecord.setAreaCode(areaCode);
//                boolean saveDjnsrxxSyncRecord=saveDjnsrxxSyncRecord(djNsrxxSyncRecord, "001", "失败");
//
//            }
        }

        return null;
    }



    //保存dj_nsrxx
    @Override
    public boolean saveDjnsrxx(DjNsrxx djNsrxx) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("shxydm",djNsrxx.getShxydm()==null?djNsrxx.getNsrsbh():djNsrxx.getShxydm());
        queryWrapper.eq("area_code",djNsrxx.getAreaCode());
        DjNsrxx old = djNsrxxService.getOne(queryWrapper);

        if (BeanUtil.isEmpty(old)){
            if(StringUtils.isBlank(djNsrxx.getShxydm())){
                djNsrxx.setShxydm(djNsrxx.getNsrsbh());
            }
            djNsrxx.setCreateTime(LocalDateTime.now());
            if(StringUtils.isNotBlank(djNsrxx.getNsrsbh())){
                return djNsrxxService.save(djNsrxx);
            }
            return false;
        }else {
            djNsrxx .setUpdateTime(LocalDateTime.now());
            if(StringUtils.isBlank(djNsrxx.getShxydm())){
                djNsrxx.setShxydm(djNsrxx.getNsrsbh());
            }
            boolean update=djNsrxxService.update(djNsrxx, queryWrapper);
            return update;
        }
    }

    //保存dj_nsrxx
    @Override
    public boolean saveDjnsrxxFx(DjNsrxx djNsrxx) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("shxydm",djNsrxx.getShxydm()==null?djNsrxx.getNsrsbh():djNsrxx.getShxydm());
        queryWrapper.eq("area_code",djNsrxx.getAreaCode());
        DjNsrxx old = djNsrxxFxService.getOne(queryWrapper);

        if (BeanUtil.isEmpty(old)){
            if(StringUtils.isBlank(djNsrxx.getShxydm())){
                djNsrxx.setShxydm(djNsrxx.getNsrsbh());
            }
            djNsrxx.setCreateTime(LocalDateTime.now());
            boolean save=djNsrxxFxService.save(djNsrxx);
            return save;
        }else {
            djNsrxx .setUpdateTime(LocalDateTime.now());
            if(StringUtils.isBlank(djNsrxx.getShxydm())){
                djNsrxx.setShxydm(djNsrxx.getNsrsbh());
            }
            boolean update=djNsrxxFxService.update(djNsrxx, queryWrapper);
            return update;
        }
    }

    //保存同步记录表
    @Override
    public boolean saveDjnsrxxSyncRecord(DjNsrxxSyncRecord djNsrxxSyncRecord, String status, String remark) {

        QueryWrapper queryWrapper = new QueryWrapper(djNsrxxSyncRecord);
        DjNsrxxSyncRecord oldRecord =this.getOne(queryWrapper);

        if (BeanUtil.isEmpty(oldRecord)){
            djNsrxxSyncRecord.setStatus(status);
            djNsrxxSyncRecord.setRemark(remark);
            djNsrxxSyncRecord.setCreateTime(LocalDateTime.now());
            boolean save = this.save(djNsrxxSyncRecord);
            return save;
        }else {
            oldRecord.setStatus(status);
            oldRecord.setRemark(remark);
            oldRecord.setUpdateTime(LocalDateTime.now());
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("id",oldRecord.getId());
            wrapper.eq("area_code",oldRecord.getAreaCode());
            boolean update = this.update(oldRecord,wrapper);
            return update;
        }
    }*/


}
